/*
 * Copyright (C) 2013 yixia.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huobao.yunying.activitys;

import java.net.URLEncoder;

import org.xutils.common.util.LogUtil;

import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnBufferingUpdateListener;
import io.vov.vitamio.MediaPlayer.OnInfoListener;
import io.vov.vitamio.Vitamio;
import io.vov.vitamio.widget.CenterLayout;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;

public class Act_VideoView extends Activity implements OnInfoListener, OnBufferingUpdateListener {

	/**
	 * TODO: Set the path variable to a streaming video URL or a local media
	 * file path.
	 */
	private String path = "http://m.zhangyingtianxia.com/data/files/video/zhaohui/%E6%AF%8D%E5%A9%B4%E5%BA%97%E8%90%A5%E8%BF%90%E6%8A%80%E5%B7%A7%E8%B5%B5%E8%BE%89-1.mp4";
	private Uri uri;
	private VideoView mVideoView;
	private CenterLayout layout;
	// private ProgressBar pb;
	// private TextView downloadRateView, loadRateView;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Vitamio.isInitialized(getApplicationContext());

		setContentView(R.layout.videobuffer);
		layout = (CenterLayout) findViewById(R.id.layout);
		mVideoView = (VideoView) findViewById(R.id.buffer);
		// pb = (ProgressBar) findViewById(R.id.probar);
		//
		// downloadRateView = (TextView) findViewById(R.id.download_rate);
		// loadRateView = (TextView) findViewById(R.id.load_rate);
		initDialog();
		path = getIntent().getStringExtra("path");
		path = URLEncoder.encode(path);
		path = path.replaceAll("%3A", ":").replaceAll("%2F", "/");
		LogUtil.e(path);

		if (path == "") {
			// Tell the user to provide a media file URL/path.
			return;
		} else {
			/*
			 * Alternatively,for streaming media you can use
			 * mVideoView.setVideoURI(Uri.parse(URLstring));
			 */
			showloading();
			uri = Uri.parse(path);
			mVideoView.setVideoURI(uri);
			MediaController controller = new MediaController(this);
			mVideoView.setMediaController(new MediaController(this));
			mVideoView.requestFocus();
			mVideoView.setOnInfoListener(this);
			mVideoView.setOnBufferingUpdateListener(this);
			mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mediaPlayer) {
					// optional need Vitamio 4.0
					dismissLoading();
					mediaPlayer.start();
					mediaPlayer.setPlaybackSpeed(1.0f);
				}
			});
		}

	}


	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		switch (what) {
		case MediaPlayer.MEDIA_INFO_BUFFERING_START:
			if (mVideoView.isPlaying()) {
				mVideoView.pause();
				// pb.setVisibility(View.VISIBLE);
				// downloadRateView.setText("");
				// loadRateView.setText("");
				// downloadRateView.setVisibility(View.VISIBLE);
				// loadRateView.setVisibility(View.VISIBLE);
				LogUtil.e("开始缓冲------");
				tv_speed.setText("");
				showloading();
				setDialogText("缓冲中...0%");
			}
			break;
		case MediaPlayer.MEDIA_INFO_BUFFERING_END:
			mVideoView.start();
			// pb.setVisibility(View.GONE);
			// downloadRateView.setVisibility(View.GONE);
			// loadRateView.setVisibility(View.GONE);
			LogUtil.e("缓冲完成------");
			dismissLoading();
			break;
		case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
			// downloadRateView.setText("" + extra + "kb/s" + " ");
			LogUtil.e("下载速度-----" + extra);
			tv_speed.setText("" + extra + "kb/s" + "  ");
			break;
		}
		return true;
	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		// loadRateView.setText(percent + "%");
		setDialogText("缓冲中..." + percent + "%");
		LogUtil.e("缓冲了-----" + percent + "%");
	}

	private ProgressDialog dialog;
	private View dialogView;
	private TextView tv_msg, tv_speed;

	private void initDialog() {
		dialog = new ProgressDialog(this);
		dialogView = LayoutInflater.from(this).inflate(R.layout.layout_loading_h, null);
		tv_msg = (TextView) dialogView.findViewById(R.id.tv_1);
		tv_speed = (TextView) dialogView.findViewById(R.id.tv_2);
	}

	private void showloading() {
		if (dialog != null && dialog.isShowing() == false) {
			dialog.show();
			dialog.setContentView(dialogView);
		}
	}

	private void setDialogText(String text) {
		if (dialog != null && dialog.isShowing() == false) {
			tv_msg.setText(text);
		}
	}

	private void dismissLoading() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
	}
}
